<?php

namespace App\Http\Controllers;

use App\Exports\UsersExport;
use App\Exports\UsersExportForArr;
use App\Post;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

#测试控制器 http://blog.martin.net/tester/wubuze
class Tester extends Controller {

    public function wubuze()
    {

        $post = Post::table();
        dd($post);
        return 'test';


        $a = 0;
        $b = $a + 1;
        return $b;

        return 1;
//        return Excel::download(new UsersExport, 'users.xlsx');
//        $res =  Excel::store(new UsersExport, 'users.xlsx');


        #传入数组导出excel
        $export = new UsersExportForArr([
            [1,2,3],
            [3,2,1]
        ]);
        return Excel::download($export, 'users.xlsx');
        return $excel->download($usersExport, 'user.csv');

        return 'wbz';
    }

    public function showLog(Request $req) {
        $date = $req->date;
        if (!$date) {
            $date = date('Y-m-d');
        }
        $file = base_path('storage/logs/laravel-'.$date.'.log') ;

        if ($req->get('act')=='delete') {
            file_put_contents($file, '');
            return 'Log 文件已清除。';
        }

        return "日志文件 $date <br>".file_get_contents($file);
    }

    /**
     * export
     */
    public function exportExcel(Excel $excel, UsersExport $usersExport)
    {

        #文件转为二进制给前端
//         $binary = file_get_contents(storage_path('app/users.xlsx'));
//        return response($binary,200)
//            ->header('Content-Type','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        //        return Excel::download(new UsersExport, 'users.xlsx');
//        $res =  Excel::store(new UsersExport, 'users.xlsx');
        /*$export = new UsersExportForArr([
            [1,2,3],
            [3,2,1]
        ]);*/
        return Excel::download($usersExport, 'users.xlsx');//二进制文件，前端使用file 对象接受直接打开。
//        return Excel::store($usersExport, 'user.csv');



    }

    /**
     * import
     */
    public function importExcel(\App\Imports\UserImport $import)
    {

        $file =  request()->file('file');
//        Excel::import($import, 'users.xlsx');//从默认磁盘导入
//        Excel::import($import, request()->file('file'));//用户上传

        #导入到数组或集合
//       return $array = Excel::toArray($import, $file);
     return   $collection = Excel::toCollection($import, $file);

        return 'ok';
    }



}
